<template>
  <view>
    <!-- 模块1：收货地址 -->
    <view class="address">
      <!-- 正常情况：显示收货地址 -->
      <view v-if="address.userName" class="address-info">
        <view class="address-detail">{{ detailAddress }}</view>
        <view class="address-user"> {{ address.userName }} {{ address.telNumber }} </view>
      </view>
      <!-- 互斥情况：显示添加按钮 -->
      <view v-else class="address-btn" @click="addAddress">添加地址</view>
    </view>
    <!-- 模块2：已选商品列表 -->
    <view class="goods-list">
      <view class="goods-title">已选商品</view>
      <view class="cart-item" v-for="item in selectGoodsList" :key="item.goods_id">
        <view class="cart-count"> x{{ item.goods_count }} </view>
        <navigator
          class="goods-item"
          hover-class="none"
          :url="`/pages/index/index`"
        >
          <!-- 商品图片 -->
          <image
            class="goods-image"
            :src="item.goods_small_logo"
            mode="aspectFit"
          />
          <!-- 商品信息 -->
          <view class="goods-info">
            <view class="goods-name">{{ item.goods_name }}</view>
            <view class="goods-price">{{ item.goods_price }}</view>
          </view>
        </navigator>
      </view>
    </view>
    <!-- 模块3：底部操作栏 -->
    <view class="bottom-bar">
      <view class="bottom-bar-fixed">
        <!-- 合计 -->
        <view class="total">
          <text class="total-name">合计：</text>
          <text class="total-price">{{ totalPrice }}</text>
        </view>
        <!-- 结算 -->
        <view class="account" @click="gotoPay"> 去支付({{ totalCount }}) </view>
      </view>
    </view>
  </view>
</template>

<script>
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      address: {}
    }
  },
  computed: {
    ...mapGetters(['selectGoodsList', 'totalCount', 'totalPrice']),
    detailAddress() {
      if (!this.address.provinceName) {
        return ''
      }
      // 当对象点了两次以上，就要注意对象是否为undeifned的情况
      return this.address.provinceName + this.address.cityName + this.address.countyName + this.address.detailInfo
    }
  },
  methods: {
    addAddress() {
      uni.chooseAddress({
        success: (res) => {
          console.log(res);
          this.address = res
        }
      })
    },
    async gotoPay() {
      // 1. 判断有没有收获地址
      if(!this.address.provinceName) {
        uni.showToast({
          title: '请填写收获地址',
          icon: 'error'
        })
        return
      }

      // 2. 发请求获取订单编号
      const goods = this.selectGoodsList.map(item => ({
        goods_id: item.goods_id,
        goods_number: item.goods_count,
        goods_price: item.goods_price
      }))
      const res1 = await uni.$u.http.post('/my/orders/create', {
        order_price: this.totalPrice,
        consignee_addr: this.detailAddress,
        goods,
      })
      console.log(res1.data.message?.order_number);

      // 3. 根据订单编号发请求，获取支付需要的5个参数
      const res2 = await uni.$u.http.post('/my/orders/req_unifiedorder', {
        order_number: res1.data.message?.order_number
      })

      console.log(res2);

      // 4. 微信支付api接口调用
      // 微信小程序要支付，就是用一个原生api就可以了， 但是这个api需要5个参数，我们需要调公司自己后端java他们写的接口，得到5个参数，在来掉微信的api
      uni.requestPayment(res2.data.message.pay)
    }
  }
}
</script>

<style lang="scss">
page {
  background-color: #f4f4f4;
}
// 模块1：收货地址
.address {
  background-color: #fff;
  margin: 20rpx;
  padding: 20rpx;
  border-radius: 10rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  .address-info {
    width: 100%;
    .address-detail {
      font-size: 30rpx;
      font-weight: bold;
      margin-bottom: 20rpx;
    }
  }
  .address-btn {
    width: 303rpx;
    height: 90rpx;
    border-radius: 45rpx;
    display: flex;
    justify-content: center;
    align-items: center;
    border: 1px solid #eb4450;
    color: #eb4450;
  }
}
// 别人面试的时候，问你怎么样处理不同品台的差异？
/* #ifdef H5 */
.address {
  background: #eb4450;
}
/* #endif */
/* #ifdef MP-WEIXIN */
.address {
  background: #43f08e;
}
/* #endif */

// 模块2：已选商品列表
.goods-list {
  background-color: #fff;
  margin: 20rpx;
  padding: 20rpx;
  border-radius: 10rpx;
  .goods-title {
    line-height: 40rpx;
    font-size: 26rpx;
  }
  .cart-item {
    position: relative;
    .cart-count {
      position: absolute;
      right: 20rpx;
      bottom: 30rpx;
    }
    // 选中最后一项
    &:last-child {
      .goods-item {
        border: none;
      }
    }
  }
}
// 全局公共样式
.goods-list {
  .goods-item {
    display: flex;
    border-bottom: 1rpx solid #ddd;
    padding: 25rpx;
    .goods-image {
      width: 190rpx;
      height: 190rpx;
    }
    .goods-info {
      flex: 1;
      font-size: 24rpx;
      // 垂直方向两端对齐
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 20rpx;
      .goods-name {
        // 多行超出隐藏
        overflow: hidden;
        -webkit-line-clamp: 2;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
      }
      .goods-price {
        color: #eb4450;
        // 前面的价格符号
        &::before {
          content: '￥';
          font-size: 80%;
          margin-right: 5rpx;
        }
      }
    }
  }
}

// 底部操作栏
.bottom-bar {
  // 占位用
  height: 100rpx;
  // 定位的操作栏
  .bottom-bar-fixed {
    height: 80rpx;
    position: fixed;
    z-index: 99;
    left: 0;
    right: 0;
    bottom: 0;

    border-top: 1px solid #eee;
    background-color: #fff;
    display: flex;
    align-items: center;
    padding: 0 20rpx;

    .select {
      .select-radio {
        font-size: 24rpx;
        color: #999;
      }
    }

    .total {
      flex: 1;
      display: flex;
      align-items: center;
      margin-left: 40rpx;
      .total-name {
        font-size: 24rpx;
      }

      .total-price {
        color: #eb4450;
      }
    }

    .account {
      background-color: #eb4450;
      color: #fff;
      font-size: 22rpx;
      width: 150rpx;
      height: 52rpx;
      border-radius: 26rpx;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>